require("mason").setup({
  github = {
    -- The template URL to use when downloading assets from GitHub.
    -- The placeholders are the following (in order):
    -- 1. The repository (e.g. "rust-lang/rust-analyzer")
    -- 2. The release version (e.g. "v0.3.0")
    -- 3. The asset name (e.g. "rust-analyzer-v0.3.0-x86_64-unknown-linux-gnu.tar.gz")
    download_url_template = "https://mirror.ghproxy.com/https://github.com/%s/releases/download/%s/%s",
  },
})

-- https://github.com/folke/trouble.nvim/issues/96
-- 设置lsp图标
local signs = { Error = "", Warn = "", Hint = "", Info = "" }
for type, icon in pairs(signs) do
  local hl = "DiagnosticSign" .. type
  vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end

require("mason-lspconfig").setup()

local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- capabilities.offsetEncoding = "utf-8" -- hide warning: multiple different client offset_encodings detected for buffer, this is not supported yet

-- lsp winbar
local navic = require("nvim-navic")

-- 自动setup语言服务器
require("mason-lspconfig").setup_handlers({
  -- The first entry (without a key) will be the default handler
  -- and will be called for each installed server that doesn't have
  -- a dedicated handler.
  function(server_name) -- default handler (optional)
    require("lspconfig")[server_name].setup({
      capabilities = capabilities,
      on_attach = function(client, bufnr)
        if client.server_capabilities.documentSymbolProvider then
          navic.attach(client, bufnr)
        end
      end,
    })
  end,
  -- Next, you can provide a dedicated handler for specific servers.
  -- For example, a handler override for the `rust_analyzer`:
  -- ["rust_analyzer"] = function ()
  --     require("rust-tools").setup {}
  -- end
})

-- 手动设置部分lsp
require("lspconfig").dartls.setup({
  capabilities = capabilities,
  on_attach = function(client, bufnr)
    if client.server_capabilities.documentSymbolProvider then
      navic.attach(client, bufnr)
    end
  end,
})

require("lsp_signature").setup({
  bind = true, -- This is mandatory, otherwise border config won't get registered.
  handler_opts = {
    border = "rounded",
  },
})

require("mason-nvim-dap").setup({
  handlers = {
    function(config)
      require("mason-nvim-dap").default_setup(config)
      local dap, dapui = require("dap"), require("dapui")
      dapui.setup()
      dap.listeners.after.event_initialized["dapui_config"] = function()
        dapui.open()
      end
      dap.listeners.before.event_terminated["dapui_config"] = function()
        dapui.close()
      end
      dap.listeners.before.event_exited["dapui_config"] = function()
        dapui.close()
      end
    end,
    -- python = function(config)
    --     config.adapters = {
    --         type = "executable",
    --         command = "/usr/bin/python3",
    --         args = {
    --             "-m",
    --             "debugpy.adapter",
    --         },
    --     }
    --     require('mason-nvim-dap').default_setup(config) -- don't forget this!
    -- end,
  },
})
